





// Find the best icon with highest resolution for the launcher
QString find_icon ( QString icon )
{
    QStringList iconbase        = ( 'default' , 'hicolor' , 'gnome' ) ;
    QString     iconpath        = "/usr/share/icons/" ;
    QStringList sizelist        = ('256x256', '128x128', '64x64', '48x48', '32x32', '24x24') ;
    QStringList categorylist    = ( 'apps' , 'devices' , 'categories' , 'filesystems' , 'places' , 'actions' ) ;
    QStringList extensionlist   = ( 'png' , 'xpm' ) ;

    QString     foundiconfile ;
    QStringList iconimagelist ;

    foreach ( QString extension , extensionlist )
    {
        if ( icon.find ( QString(".%1").arg(extension) ) != -1 )
        {
            icon = icon.replace ( QString(".%1").arg(extension) , QString() ) ;
        }
    }

    foreach ( QString size , sizelist )
    {
        foreach ( QString extension , extensionlist )
        {
            foreach ( QString category , categorylist )
            {
                foreach ( QString iconbasecat , iconbase )
                {
                    QString iconfile = QString ("%1/%2/%3/%4/%5.%6").arg(iconpath).arg(iconbasecat).arg(size).arg(category).arg(icon).arg(extension) ;
                    iconimagelist.append(iconfile) ;
                }
            }
        }
    }

    foreach ( QString extension , extensionlist )
    {
        QString iconfile = QString ("/usr/share/pixmaps/%1.%2").arg(icon).arg(extension) ;
        iconimagelist.append(iconfile) ;
    }

    // Seek if the files in pre-generated list exists.. first match is the best
    // return it
    foreach ( QString iconimage , iconimagelist )
    {
        if ( QFile(iconimage).exists() )
        {
            // qDebug() <<  iconimage ;
            foundiconfile = iconimage ;
            // qDebug() << "Found: " << foundiconfile ;
            return foundiconfile ;
        }
    }

    return foundiconfile ;
}
